<?php

namespace App\Http\Controllers\Open\v1;

use App\Models\Customer\Customer;
use App\Models\Panx\Product;
use App\Services\Utils\Uuid;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CustomerController extends Controller
{
    /**
     * 为客户创建访问开放平台的askey
     * @param Request $request
     * @return json string
     */
    public function getOrCreateOpenASKey(Request $request)
    {
        $code = 200;
        $error = null;
        $data =[];
        if (!$request->has('customer_id')){
            $code = 4001;
            $error='customer_id不能为空';
            return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$data]);
        }

        $customer = Customer::find($request->customer_id);
        if (!isset($customer)){
            $code = 4002;
            $error='customer_id有误';
            return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$data]);
        }

        if ($customer->open_askey == '' || $customer->open_askey==null){
            $customer->open_askey=Uuid::generate();
            $customer->save();
        }
        $data['id']=$customer->id;
        $data['name']=$customer->name;
        $data['open_askey']=$customer->open_askey;
        return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$data]);
    }

    /**
     * 获取客户名下的所有的产品
     * @param Request $request
     * @return json
     */
    public function products(Request $request)
    {
        $code = 200;
        $error = null;
        $data =[];
        if (!$request->has('open_askey')){
            $code = 4001;
            $error='open_askey不能为空';
            return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$data]);
        }

        $customer = Customer::where('open_askey',$request->open_askey)->first();
        if (!isset($customer)){
            $code = 4002;
            $error='open_askey有误';
            return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$data]);
        }

        $paginatenable=false;
        $per_page = 10;
        $productHiddenFilter=['customer_id','created_at','updated_at','utc_created_at'];

        // 分页
        if (!$request->has('paginate') || $request->paginate == '1' || $request->paginate == 1){
            $paginatenable=true;
            if ($request->has('per_page')){
                $per_page = intval($request->per_page);
            }
        }

        if ($paginatenable){ //分页操作
            $products_pag = $customer->products()
                                ->paginate($per_page);
            $data['current_page']=$products_pag->currentPage();
            $data['last_page']=$products_pag->lastPage();
            $data['first_page_url']=$products_pag->url(1);
            $data['prev_page_url']=$products_pag->previousPageUrl();
            $data['next_page_url']=$products_pag->nextPageUrl();
            $data['last_page_url']=$products_pag->url($products_pag->lastPage());
            $data['last_page']=$products_pag->lastPage();
            $data['total']=$products_pag->total();
            $data['to']=$products_pag->perPage()*$products_pag->currentPage();
            $data['per_page']=$products_pag->perPage();

            $data['products']=$products_pag->makeHidden($productHiddenFilter)->toArray();

        }else{
            $data['products']=$customer->products->makeHidden($productHiddenFilter)->toArray();
        }



        return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$data]);
    }



    /**
     * 获取指定的产品的试戴次数
     * @param Request $request
     * @return json
     */
    public function productsTryOn(Request $request)
    {
        $code = 200;
        $error = null;
        $data =[];
        if (!$request->has('product_id')){
            $code = 4001;
            $error='product_id不能为空';
            return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$data]);
        }

        $product = Product::where('id', $request->product_id)->first();
        if (!isset($product)){
            $code = 4002;
            $error='product_id';
            return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$data]);
        }

        $data['product_id']=$product->id;
        $data['try_on']=$product->try_on;

        return json_encode(['code'=>$code, 'error'=>$error, 'data'=>$data]);
    }


}
